package io.mqttpush.getway.http.controller;

import com.alibaba.fastjson.JSON;
import io.mqttpush.getway.beans.HttpPushVo;
import io.mqttpush.getway.common.CommonResponse;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpResponse;

import java.nio.charset.Charset;

/**
 * 处理JSON的fulltext
 * 
 * @author tianzhenjiu
 *
 */
public class FullTextController extends Controller {

	protected FullTextController() {
		initBcChannel();
	}

	@Override
	public HttpResponse service0(Channel requestChannel, HttpRequest request,HttpResponse response) throws Exception{

		if (!(request instanceof FullHttpRequest)) {
			return CommonResponse.badResponse();
		}

		FullHttpRequest fullHttpRequest = (FullHttpRequest) request;

		ByteBuf byteBuf = fullHttpRequest.content();

		byte[] bs = new byte[byteBuf.readableBytes()];
		byteBuf.readBytes(bs);

		HttpPushVo httpPushVo = JSON.parseObject(new String(bs,Charset.forName("utf-8")),
				HttpPushVo.class);

		if(logger.isDebugEnabled()&&httpPushVo!=null) {
			logger.debug("JSON序列化成功"+httpPushVo.toString());
		}


		response=super.routeData(requestChannel, httpPushVo,request,response);

		return response;
	}

}
